package com.cennavi.minemap.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.bean.LayerBean;
import com.cennavi.minemap.bean.LayerPerfixTreeBean;
import com.cennavi.minemap.bean.LayerSortBean;
import com.cennavi.minemap.bean.LayerWithPrefixBean;
import com.cennavi.minemap.bean.UpdateLayerStyleBean;
import com.cennavi.minemap.dao.LayerDao;
import com.cennavi.minemap.util.StringUtil;

@Service
@ComponentScan(basePackages = { "com.cennavi.minemap.dao" })
public class LayerService {
	@Autowired
	private LayerDao layerDao;

	public List<LayerWithPrefixBean> insert(LayerBean layerBean) {
		String id = StringUtil.uuid().replace("-", "");
		layerBean.setId(id);
		layerDao.insert(layerBean);
		return Arrays.asList(layerDao.queryById(id));
	}
	
	public void insertUnReturn(LayerWithPrefixBean layerBean) {
		String id = StringUtil.uuid().replace("-", "");
		layerBean.setId(id);
		layerDao.insert(layerBean);
	}

	public int update(LayerBean layerBean) {
		return layerDao.update(layerBean);
	}

	public int delete(String id) {
		return layerDao.delete(id);
	}

	public int updateStyle(String field, UpdateLayerStyleBean updateLayerStyleBean) {

		String id = updateLayerStyleBean.getId();
		String k = updateLayerStyleBean.getK();
		String v = updateLayerStyleBean.getV();

		LayerWithPrefixBean layerBean = layerDao.queryById(id);
		String content = layerBean.getContent();
		JSONObject json = JSONObject.parseObject(content);

		if (field.equals("minzoom") || field.equals("maxzoom")) {
			try {
				double dv = Double.parseDouble(v); // modify by zhangjin
													// 调用没有传入Key，用field为Key;
				json.put(field, dv);
			} catch (Exception e) {
				json.put(field, v);
			}

		} else if (field.equals("filter")) {
			try {
				JSONArray vjsonA = JSONArray.parseArray(v);
				json.put(field, vjsonA);
			} catch (Exception e2) {

			}
		}

		else {
			JSONObject fieldJson = json.getJSONObject(field);
			if (fieldJson == null) {
				fieldJson = new JSONObject();
			}

			try {
				JSONObject vjsonO = JSONObject.parseObject(v);
				fieldJson.put(k, vjsonO);
			} catch (Exception e1) {
				try {
					JSONArray vjsonA = JSONArray.parseArray(v);
					fieldJson.put(k, vjsonA);
				} catch (Exception e2) {
					try {
						double dv = Double.parseDouble(v);
						fieldJson.put(k, dv);
					} catch (Exception e3) {
						if ("true".equals(v)) { // add by zhangjin 增加json
												// Boolean类型判断
							fieldJson.put(k, true);
						} else if ("false".equals(v)) {
							fieldJson.put(k, false);
						} else {
							fieldJson.put(k, v);
						}
					}
				}
			}
			json.put(field, fieldJson);
		}
		return layerDao.updateStyle(id, json.toJSONString());
	}
	
	@Transactional
	public int updateStyle(String field, List<UpdateLayerStyleBean> updateLayerStyleBeans) {
		int allret = 0;
		for(UpdateLayerStyleBean updateLayerStyleBean : updateLayerStyleBeans){
			String id = updateLayerStyleBean.getId();
			String k = updateLayerStyleBean.getK();
			String v = updateLayerStyleBean.getV();

			LayerWithPrefixBean layerBean = layerDao.queryById(id);
			String content = layerBean.getContent();
			JSONObject json = JSONObject.parseObject(content);

			if (field.equals("minzoom") || field.equals("maxzoom")) {
				try {
					double dv = Double.parseDouble(v); // modify by zhangjin
														// 调用没有传入Key，用field为Key;
					json.put(field, dv);
				} catch (Exception e) {
					json.put(field, v);
				}

			} else if (field.equals("filter")) {
				try {
					JSONArray vjsonA = JSONArray.parseArray(v);
					json.put(field, vjsonA);
				} catch (Exception e2) {

				}
			}

			else {
				JSONObject fieldJson = json.getJSONObject(field);
				if (fieldJson == null) {
					fieldJson = new JSONObject();
				}

				try {
					JSONObject vjsonO = JSONObject.parseObject(v);
					fieldJson.put(k, vjsonO);
				} catch (Exception e1) {
					try {
						JSONArray vjsonA = JSONArray.parseArray(v);
						fieldJson.put(k, vjsonA);
					} catch (Exception e2) {
						try {
							double dv = Double.parseDouble(v);
							fieldJson.put(k, dv);
						} catch (Exception e3) {
							if ("true".equals(v)) { // add by zhangjin 增加json
													// Boolean类型判断
								fieldJson.put(k, true);
							} else if ("false".equals(v)) {
								fieldJson.put(k, false);
							} else {
								fieldJson.put(k, v);
							}
						}
					}
				}
				json.put(field, fieldJson);
			}
			int ret = layerDao.updateStyle(id, json.toJSONString());
			allret = allret + ret; 
		}
		return allret;
	}

	public List<LayerWithPrefixBean> queryBySoluId(int soluId) {
		return layerDao.queryBySoluId(soluId);
	}

	public List<LayerWithPrefixBean> queryById(String id) {
		return Arrays.asList(layerDao.queryById(id));
	}

	public int sort(List<LayerSortBean> layerSort) {
		return layerDao.updateLayerSort(layerSort);
	}

	public int updateGroup(final String groupId, final String groupName, String[] ids) {
		return layerDao.updateGroup(groupId, groupName, ids);
	}
	
	public List<LayerPerfixTreeBean> queryLayerPerfixTreeByCode(String code) {
		return layerDao.queryLayerPerfixTreeByCode(code);
	}
	
	public List<LayerPerfixTreeBean> queryLayerPerfixTreeByFather(String father) {
		return layerDao.queryLayerPerfixTreeByFather(father);
	}
	public int updateLayersPrefix(final String prefix, String[] ids){
		return layerDao.updateLayersPrefix(prefix, ids);
	}
}
